package com.lsh.payment.core.util;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by wuhao on 2019/6/14.
 */
public class StringTool {
    private static final Pattern linePattern = Pattern.compile("_(\\w)");

    private static Logger logger = LoggerFactory.getLogger(StringTool.class);
    private static Pattern humpPattern = Pattern.compile("[A-Z]");

    /** 下划线转驼峰 */
    public static String lineToHump(String str) {
        str = str.toLowerCase();
        Matcher matcher = linePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    /** 驼峰转下划线(简单写法，效率低于{@link #humpToLine2(String)}) */
    public static String humpToLine(String str) {
        str =  str.replaceAll("[A-Z]", "_$0").toLowerCase();
        if(str.subSequence(0,1).equals("_")){
            return str.substring(1,str.length());
        }
        return str;
    }

    /** 驼峰转下划线,效率比上面高 */
    public static String humpToLine2(String str) {
        Matcher matcher = humpPattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    public static String listToString(List list, char separator) {
        return StringUtils.join(list.toArray(), separator);
    }

    public static void main(String[] args) throws Exception{
        Map<String,String> params = new HashMap<>();
        params.put("a_d", "ddddd");
        params.put("b_w", "{\"s_qqq\":\"ddddddd\"}");
        params.put("ddd_d_da","[{\"dddd_eeee\":\"ddddddddddd\"},{\"iiee_eeq\":{\"eewewq_dadawdw\":\"dadwadwadawdawdwadwadwadwa\"}}]");
    }
}